package com.nandi.deviceinstall.weight.imagepicker;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.RequestOptions;
import com.lzy.imagepicker.bean.ImageItem;
import com.nandi.deviceinstall.R;
import com.nandi.deviceinstall.common.Constant;
import com.nandi.deviceinstall.util.SharedTool;

import java.util.ArrayList;
import java.util.List;

public class ImagePickerNoAddAdapter extends RecyclerView.Adapter<ImagePickerNoAddAdapter.SelectedPicViewHolder> {
    private int                             maxImgCount;
    private Context                         mContext;
    private List<ImageItem>                 mData;
    private LayoutInflater                  mInflater;
    private OnRecyclerViewItemClickListener listener;
    private boolean                         isAdded = false;

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.listener = listener;
    }

    public void setImages(List<ImageItem> data) {
        mData = new ArrayList<>(data);
        /*if (getItemCount() < maxImgCount) {
            mData.add(new ImageItem());
            isAdded = true;
        } else {
            isAdded = false;
        }*/
//        isAdded = "2".equals(userType) ? false : isAdded;
        notifyDataSetChanged();
    }
    public List<ImageItem> getImages() {
        //由于图片未选满时，最后一张显示添加图片，因此这个方法返回真正的已选图片

        if (isAdded){
            return new ArrayList<>(mData.subList(0, mData.size() - 1));
        }else {
            return mData;
        }
    }

    public ImagePickerNoAddAdapter(Context mContext, List<ImageItem> data, int maxImgCount) {
        this.mContext = mContext;
        this.maxImgCount = maxImgCount;
        this.mInflater = LayoutInflater.from(mContext);
        setImages(data);
    }

    @Override
    public SelectedPicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectedPicViewHolder(mInflater.inflate(R.layout.list_item_image, parent,false));
    }

    @Override
    public void onBindViewHolder(SelectedPicViewHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class SelectedPicViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView iv_img;
        private int       clickPosition;

        public SelectedPicViewHolder(View itemView) {
            super(itemView);
            iv_img = (ImageView) itemView.findViewById(R.id.iv_img);
        }

        public void bind(int position) {
            //设置条目的点击事件
            itemView.setOnClickListener(this);
            //根据条目位置设置图片
            ImageItem item = mData.get(position);
            if (isAdded && position == getItemCount() - 1) {
                iv_img.setImageResource(R.drawable.ic_add_img);
                clickPosition = PictureCodeKt.IMAGE_ITEM_ADD;
            } else {
//                ImagePicker.getInstance().getImageLoader().displayImage((Activity) mContext, item.path, iv_img, 0, 0);

                //Authorization 请求头信息
        LazyHeaders headers = new LazyHeaders.Builder().addHeader("Authorization", (String) SharedTool.get(mContext, Constant.TOKEN, "")).build();
                RequestOptions requestOptions = new RequestOptions().error(R.mipmap.default_image)
                        .placeholder(R.mipmap.default_image).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true);
                Glide.with(mContext)
                        //.asBitmap()
                        //.load(item.path)
                        .load(new GlideUrl(item.path, headers))
                        /*//不使用缓存
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        //设置错误图片
                        .error(R.mipmap.default_image)
                        //设置占位图片
                        .placeholder(R.mipmap.default_image)*/
                        .apply(requestOptions)
                        .into(iv_img);
                clickPosition = position;
            }
        }

        @Override
        public void onClick(View v) {
            if (listener != null) listener.onItemClick(v, clickPosition);
        }
    }
}